﻿<?php

/**
 * O2O团购PC版本
 * ============================================================================
 * * 版权所有 2009-2016 郑州司科计算机科技有限公司，并保留所有权利。
 * 购买网址: http://www.jzphp.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
  * 咨询电话：400-823-0371 QQ：313999875
 * ============================================================================
 * $Author: zzsike $
 * $Time 2016-02-03 sammy $
*/


require_once(dirname(dirname(__FILE__)) . '/app.php');
$daytime = strtotime(date('Y-m-d'));
$condition = array(
	'team_type' => 'normal',
	'city_id' => array(0, abs(intval($city['id']))),
	"begin_time <= '{$daytime}'",
	"end_time > '{$daytime}'",
	'business_audit' => array('C','T'),
	
);
$p=1;
$group_id = abs(intval(dowith_sql($_GET['gid'])));
if ($group_id) $condition['group_id'] = $group_id;
$size=24;

$city_id = abs(intval(dowith_sql($_GET['cid'])));
if ($city_id) $condition['city_id'] = $city_id;

/*按价格筛选*/
	$selector_p = strval(dowith_sql($_GET['p']));
if ( $selector_p == 'p0' ) {
	$condition[$group_id] = array(
	'team_type' => 'normal',
	'city_id' => array(0, abs(intval($city['id']))),
	"team_price =0",
	"begin_time <= '{$daytime}'",
	"end_time >= '{$daytime}'",
	'business_audit' => array('C','T'),
	);
}
else if ( $selector_p == 'p1' ) {
	$condition[$group_id] = array (
	'team_type' => 'normal',
	'city_id' => array(0, abs(intval($city['id']))),
	"team_price > 0 and team_price <10",
	"begin_time <= '{$daytime}'",
	"end_time >= '{$daytime}'",
	'business_audit' => array('C','T'),
	);
}
else if ( $selector_p == 'p2' ) {
	$condition[$group_id] = array (
	'team_type' => 'normal',
	'city_id' => array(0, abs(intval($city['id']))),
	"team_price >= 10 and team_price <50",
	"begin_time <= '{$daytime}'",
	"end_time >= '{$daytime}'",
	'business_audit' => array('C','T'),
	);
}
else if ( $selector_p == 'p3' ) {
	$condition[$group_id] = array (
	'team_type' => 'normal',
	'city_id' => array(0, abs(intval($city['id']))),
	"team_price >= 50 and team_price <100",
	"begin_time <= '{$daytime}'",
	"end_time >= '{$daytime}'",
	'business_audit' => array('C','T'),
	);
}
else if ( $selector_p == 'p4' ) {
	$condition[$group_id] = array (
	'team_type' => 'normal',
	'city_id' => array(0, abs(intval($city['id']))),
	"team_price >= 100 and team_price <200",
	"begin_time <= '{$daytime}'",
	"end_time >= '{$daytime}'",
	'business_audit' => array('C','T'),
	);
}
else if ( $selector_p == 'p5' ) {
	$condition[$group_id] = array (
	'team_type' => 'normal',
	'city_id' => array(0, abs(intval($city['id']))),
	"team_price >= 200 and team_price <500",
	"begin_time <= '{$daytime}'",
	"end_time >= '{$daytime}'",
	'business_audit' => array('C','T'),
	);
}
else if ( $selector_p == 'p6' ) {
	$condition[$group_id] = array (
	'team_type' => 'normal',
	'city_id' => array(0, abs(intval($city['id']))),
	"team_price >= 500 and team_price <1000",
	"begin_time <= '{$daytime}'",
	"end_time >= '{$daytime}'",
	'business_audit' => array('C','T'),
	);
}
else if ( $selector_p == 'p7' ) {
	$condition[$group_id] = array (
	'team_type' => 'normal',
	'city_id' => array(0, abs(intval($city['id']))),
	"team_price >= 1000",
	"begin_time <= '{$daytime}'",
	"end_time >= '{$daytime}'",
	'business_audit' => array('C','T'),
	);
}

/*排序*/
$count = Table::Count('team', $condition);
if($_GET['show']=='all')$size=1000;
if($_GET[page]<2)
{
  if($count>=$size)$current_count=$size;
  else $current_count=$count;
}
else
$current_count=$count-($_GET[page]-1)*$size;
list($pagesize, $offset, $pagestring) = pagestring($count, $size);
if(!empty($_GET['type']))
{
if($_GET['show']=='all')$pagesize=1000;
else
$pagesize=abs(intval($INI['system']['sideteam']));
}
if($count>$size)$show=1;
if($_GET['show']=='all')
{$show=1;$p=0;}
if($_GET['s']=="n")
{
$order='ORDER BY begin_time DESC, sort_order DESC, id DESC';
}
elseif($_GET['s']=="b")
{
$order='ORDER BY now_number DESC, sort_order DESC, id DESC';
}
elseif($_GET['s']=="pa")
{
$order='ORDER BY team_price ASC, sort_order DESC, id DESC';
}
elseif($_GET['s']=="pd")
{
$order='ORDER BY team_price  DESC, sort_order DESC, id DESC';
}
elseif($_GET['s']=="zd")
{
$order='ORDER BY team_price/market_price  DESC, sort_order DESC, id DESC';
}
elseif($_GET['s']=="za")
{
$order='ORDER BY team_price/market_price  ASC, sort_order DESC, id DESC';
}
else 
{
$order='ORDER BY  sort_order DESC,begin_time DESC, id DESC';
}

$teams = DB::LimitQuery('team', array(
	'condition' => $condition,
	'order' => $order,
	'size' => $pagesize,
	'offset' => $offset,
));
$now = time();
$detail = array();

foreach($teams AS $index => $team) {

if($team[group_id])
$team[group_name]=Table::Fetch('category', $team[group_id]);

	if($team['end_time']<$team['begin_time']){$team['end_time']=$team['begin_time'];}
	$diff_time = $left_time = $team['end_time']-$now;
	if ( $team['team_type'] == 'seconds' && $team['begin_time'] >= $now ) {
		$diff_time = $left_time = $team['begin_time']-$now;
	}

	/* progress bar size */
	$detail[$team['id']]['bar_size'] = ceil(190*($team['now_number']/$team['min_number']));
	$detail[$team['id']]['bar_offset'] = ceil(5*($team['now_number']/$team['min_number']));

	$left_day = floor($diff_time/86400);
	$left_time = $left_time % 86400;
	$left_hour = floor($left_time/3600);
	$left_time = $left_time % 3600;
	$left_minute = floor($left_time/60);
	$left_time = $left_time % 60;

	$detail[$team['id']]['diff_time'] = $diff_time;
	$detail[$team['id']]['left_day'] = $left_day;
	$detail[$team['id']]['left_hour'] = $left_hour;
	$detail[$team['id']]['left_minute'] = $left_minute;
	$detail[$team['id']]['left_time'] = $left_time;
	$detail[$team['id']]['is_today'] = $team['begin_time'] + 3600*24 > time() ? 1:0;

	/* state */
	$team['state'] = team_state($team);
	$teams[$index] = $team;
}

$category = Table::Fetch('category', $group_id);

if(empty($_GET[gid])) $indexs=1;
$gid=$_GET[gid]?$_GET[gid]:0;

/*首页分类调用*/
$teamothers_city_id = abs(intval($city['id']));
$teamothers_team_id = abs(intval($team['id']));
$teamothers_now = time();
$hot_oc = array( 
			'city_id' => array($teamothers_city_id, 0), 
			'team_type' => 'normal',
			'is_hot' => 'Y',
			"begin_time < '$teamothers_now'",
			"end_time > '$teamothers_now'",
			'business_audit' => array('C','T'),
			);
$hot_sider_oc = array( 
			'city_id' => array($teamothers_city_id, 0), 
			'team_type' => 'normal',
			'is_hot_sider' => 'Y',
			"begin_time < '$teamothers_now'",
			"end_time > '$teamothers_now'",
			'business_audit' => array('C','T'),
			);
$chosen_oc = array( 
			'city_id' => array($teamothers_city_id, 0), 
			'team_type' => 'normal',
			'is_chosen' => 'Y',
			"begin_time < '$teamothers_now'",
			"end_time > '$teamothers_now'",
			'business_audit' => array('C','T'),
			);
$oc = array( 
			'city_id' => array($teamothers_city_id, 0), 
			'team_type' => 'normal',
			"begin_time < '$teamothers_now'",
			"end_time > '$teamothers_now'",
			'business_audit' => array('C','T'),
			);
$hot_teams = DB::LimitQuery('team', array(
				'condition' => $hot_oc,
				'order' => 'ORDER BY `sort_order` DESC, `id` DESC',
				'size' => '1',
				));
$hot_sider_teams = DB::LimitQuery('team', array(
				'condition' => $hot_sider_oc,
				'order' => 'ORDER BY `sort_order` DESC, `id` DESC',
				'size' => '2',
				));
$chosen_teams = DB::LimitQuery('team', array(
				'condition' => $chosen_oc,
				'order' => 'ORDER BY `sort_order` DESC, `id` DESC',
				'size' => '4',
				));
$teamothers = DB::LimitQuery('team', array(
				'condition' => $oc,
				'order' => 'ORDER BY `sort_order` DESC, `id` DESC',
				));
$count = Table::Count('team', $condition);
$team = array_shift($teamothers);
$groups = Table::Fetch('category', Utility::GetColumn($teamothers, 'group_id'));
$citys = Table::Fetch('category', Utility::GetColumn($teamothers, 'city_id'));
$loopteam = Utility::AssColumn($teamothers, 'group_id', 'id');

if(empty($_GET[cid])) $indexs=1;
$gid=$_GET[cid]?$_GET[cid]:0;

$team = null;
$pagetitle = '往期团购';
include template('team_in');

/*价格2*/
$request_uri = 'index';

//$team = $teams = index_get_team($city['id']);

$city_id = abs(intval($city['id']));
	$now = time();
	$size = abs(intval($INI['system']['indexteam']));
	if ($size<=1) return current_team($city_id);
	$condition = array( 
			'team_type' => 'normal',
			"begin_time < '{$now}'",
			"end_time > '{$now}'",
			'business_audit' => array('C','T'),
			);
$group_id = abs(intval($_GET['gid']));

	$fid_s = abs(intval($_GET['fid_s']));
	if ($fid_s<>0){$condition['sub_id'] = $fid_s;}
	if($_GET['s']=="n")
{
$orderss='ORDER BY begin_time DESC, id DESC';
}
elseif($_GET['s']=="b")
{
$orderss='ORDER BY now_number DESC, id DESC';
}
elseif($_GET['s']=="jg")
{
$orderss='ORDER BY team_price ASC, id DESC';
}
elseif($_GET['s']=="jgd")
{
$orderss='ORDER BY team_price DESC, id DESC';
}
elseif($_GET['s']=="jg_500")
{
$condition[]= "( team_price <= 500 )";
$orderss='ORDER BY  sort_order DESC,begin_time DESC, id DESC';
}
elseif($_GET['s']=="jg_500_1000")
{
$condition[]= "( team_price > 500 and team_price<=1000 )";
$orderss='ORDER BY  sort_order DESC,begin_time DESC, id DESC';
}
elseif($_GET['s']=="jg_1000_3000")
{
$condition[]= "( team_price > 1000 and team_price<=3000 )";
$orderss='ORDER BY  sort_order DESC,begin_time DESC, id DESC';
}
elseif($_GET['s']=="jg_3000")
{
$condition[]= "( team_price > 3000 )";
$orderss='ORDER BY  sort_order DESC,begin_time DESC, id DESC';
}
elseif($_GET['s']=="zk")
{
$orderss='ORDER BY sort_order DESC, market_price - team_price ASC, id DESC';
}
elseif($_GET['s']=="zkd")
{
$orderss='ORDER BY  sort_order DESC, market_price - team_price DESC, id DESC';
}
else 
{
$orderss='ORDER BY  sort_order DESC,begin_time DESC, id DESC';
}




